﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using System.Windows.Forms;

namespace MM.Common.UI.Controls
{
    public class UCMultiListGridControl : GridControl
    {
        // Fields
        protected DataTable _DtMulitList;
        protected GridView _GV;
        private int _ListCount;
        private int _RecordCount;
        private IContainer components;

        // Methods
        public UCMultiListGridControl()
        {
            this._ListCount = 2;
            this.InitializeComponent();
        }

        public UCMultiListGridControl(IContainer container)
        {
            this._ListCount = 2;
            container.Add(this);
            this.InitializeComponent();
        }

        protected void _GV_FocusedColumnChanged(object sender, FocusedColumnChangedEventArgs e)
        {
            if (this._GV.FocusedRowHandle == (this._GV.RowCount - 1))
            {
                int tag = (int)e.FocusedColumn.Tag;
                if ((this._RecordCount % this._ListCount) <= tag)
                {
                    SendKeys.Send("+{TAB}");
                }
            }
        }

        protected void _GV_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if ((e.PrevFocusedRowHandle != e.FocusedRowHandle) && (e.FocusedRowHandle == (this._GV.RowCount - 1)))
            {
                int tag = (int)this._GV.FocusedColumn.Tag;
                if ((this._RecordCount % this._ListCount) <= tag)
                {
                    SendKeys.Send("+{TAB}");
                }
            }
        }

        public void BindData(DataTable dt)
        {
            if (this._ListCount == 1)
            {
                this._DtMulitList = dt;
            }
            else
            {
                this._DtMulitList = dt.Clone();
                for (int i = 1; i < this._ListCount; i++)
                {
                    for (int k = 0; k < dt.Columns.Count; k++)
                    {
                        DataColumn column = new DataColumn();
                        column = new DataColumn(dt.Columns[k].ColumnName, dt.Columns[k].DataType)
                        {
                            ColumnName = column.ColumnName + i.ToString()
                        };
                        this._DtMulitList.Columns.Add(column);
                    }
                }
                this._RecordCount = dt.Rows.Count;
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    int num4 = j / this._ListCount;
                    int num5 = j % this._ListCount;
                    int num6 = 0;
                    if (num5 == 0)
                    {
                        this._DtMulitList.Rows.Add(this._DtMulitList.NewRow());
                    }
                    else
                    {
                        num6 += num5 * dt.Columns.Count;
                    }
                    for (int m = 0; m < dt.Columns.Count; m++)
                    {
                        this._DtMulitList.Rows[num4][num6 + m] = dt.Rows[j][m];
                    }
                }
            }
            this.DataSource = this._DtMulitList;
        }

        protected void CopyGridColumn(GridColumn fromgc, GridColumn togc)
        {
            togc.AppearanceCell.Assign(fromgc.AppearanceCell);
            togc.AppearanceHeader.Assign(fromgc.AppearanceHeader);
            togc.Caption = fromgc.Caption;
            togc.ColumnEdit = fromgc.ColumnEdit;
            togc.ColumnEditName = fromgc.ColumnEditName;
            togc.DisplayFormat.Assign(fromgc.DisplayFormat);
            togc.ImageAlignment = fromgc.ImageAlignment;
            togc.ImageIndex = fromgc.ImageIndex;
            togc.MaxWidth = fromgc.MaxWidth;
            togc.MinWidth = fromgc.MinWidth;
            togc.OptionsColumn.Assign(fromgc.OptionsColumn);
            togc.OptionsFilter.Assign(fromgc.OptionsFilter);
            togc.ShowButtonMode = fromgc.ShowButtonMode;
            togc.SummaryItem.Assign(fromgc.SummaryItem);
            togc.ToolTip = fromgc.ToolTip;
            togc.Visible = fromgc.Visible;
            togc.Width = fromgc.Width;
        }

        public void CreateMultiList(GridView gv)
        {
            this._GV = gv;
            int count = this._GV.Columns.Count;
            int visibleIndex = 0;
            for (int i = 0; i < count; i++)
            {
                if (this._GV.Columns[i].VisibleIndex > visibleIndex)
                {
                    visibleIndex = this._GV.Columns[i].VisibleIndex;
                }
            }
            visibleIndex++;
            GridColumn[] columns = new GridColumn[count * (this._ListCount - 1)];
            for (int j = 1; j < this._ListCount; j++)
            {
                for (int k = 0; k < count; k++)
                {
                    int index = ((j - 1) * count) + k;
                    columns[index] = new GridColumn();
                    this.CopyGridColumn(this._GV.Columns[k], columns[index]);
                    columns[index].FieldName = this._GV.Columns[k].FieldName + j.ToString();
                    columns[index].Name = this._GV.Columns[k].Name + j.ToString();
                    columns[index].Width = this._GV.Columns[k].Width;
                    columns[index].Caption = this._GV.Columns[k].Caption;
                    columns[index].Tag = j;
                    this._GV.Columns[k].Tag = 0;
                    if (this._GV.Columns[k].VisibleIndex != -1)
                    {
                        GridColumn column1 = columns[index];
                        column1.VisibleIndex += visibleIndex + (this._ListCount * (j - 1));
                    }
                }
            }
            this._GV.Columns.AddRange(columns);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public DataTable GetDataTable()
        {
            DataTable table = new DataTable();
            table = this._DtMulitList.Clone();
            int num = table.Columns.Count / this._ListCount;
            for (int i = table.Columns.Count - 1; i >= num; i--)
            {
                table.Columns.Remove(table.Columns[i]);
            }
            for (int j = 0; j < this._DtMulitList.Rows.Count; j++)
            {
                for (int k = 0; k < this._DtMulitList.Columns.Count; k++)
                {
                    int num5 = k % num;
                    if (num5 == 0)
                    {
                        if (table.Rows.Count >= this._RecordCount)
                        {
                            break;
                        }
                        table.Rows.Add(table.NewRow());
                    }
                    table.Rows[table.Rows.Count - 1][num5] = this._DtMulitList.Rows[j][k];
                }
            }
            return table;
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        // Properties
        [Description("List数"), Browsable(true), Category("MM附加")]
        public int ListCount
        {
            get
            {
                return this._ListCount;
            }
            set
            {
                this._ListCount = value;
                if (this._ListCount <= 0)
                {
                    this._ListCount = 2;
                }
            }
        }
    }
}
